//
//  MainViewController.swift
//  SwiftWeiBo
//
//  Created by wyw on 16/9/16.
//  Copyright © 2016年 Style_Y. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 添加子控制器
        addChildViewController()
    }
    // 设置frame最好在viewWillAppear中设置，因为viewDidLoad在执行的时候view不已经有加载
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 添加tabbar中间button
        setupComposeBtn()
    }
    
    // MARK: - 添加按钮
    /**
     添加中间按钮
     */
    private func setupComposeBtn() {
      // 1.添加+号按钮
        tabBar.addSubview(composeBtn)
      // 2.调整位置
        let width = UIScreen.main.bounds.size.width / CGFloat(viewControllers!.count)
        let X = 2*width
        let rect = CGRect(x: X, y: 0, width: width, height: 49)
        composeBtn.frame = rect
    }
    
    func composeBtnClick() {
        
    }
    
    /**
     添加控制器
     */
    private func addChildViewController()
    {
        // 动态添加控制器
        // 1.获取json数据
        let path = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil)
        if path != nil
        {
            do {
                let jsonData = try Data(contentsOf: URL.init(fileURLWithPath: path!))
                
                do {
                    let dictArr = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers)
                    for dict in dictArr as![[String:String]] {
                        // 添加控制器
                        addChildViewController(childControllerName: dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
                    }
                } catch {
                    
                    // 添加控制器
                    addChildViewController(childControllerName: "HomeTableViewController", title: "首页", imageName: "tabbar_home")
                    addChildViewController(childControllerName: "MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
                    addChildViewController(childControllerName: "NullViewController", title: "", imageName: "")
                    addChildViewController(childControllerName: "DiscoverTableViewController", title: "广场", imageName: "tabbar_home")
                    addChildViewController(childControllerName: "ProfileTableViewController", title: "我", imageName: "tabbar_profile")
                    
                }
            } catch {
                print(error)
            }
        }

    }
    private func addChildViewController(childControllerName: String , title:String , imageName:String) {
        
        // 1.动态获取命名空间，结合控制器名字创建控制器
         // 1.1获取命名空间
        let name:String = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
         // 1.2根据类全名创建控制器
        let cls:AnyClass? = NSClassFromString(name + "." + childControllerName)!
         // 1.3强转为指定控制器
        let vcCls = cls as! UIViewController.Type
         // 1.4创建控制器
        let vc = vcCls.init()

        // 2.设置控制器
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        vc.title = title
        
        // 3.包装导航控制器
        let Nav = UINavigationController()
        Nav.addChildViewController(vc)
        
        // 4.添加到tabbarController
        addChildViewController(Nav)
    }
    
    // MARK - 懒加载
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
        btn.setImage(UIImage.init(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
        
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: UIControlState.normal)
        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
        
        // 添加点击事件
        btn.addTarget(self, action: #selector(MainViewController.composeBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
}
